import config
import time
from dateutil import rrule
from datetime import datetime
from utils import *


def register():
	return DateDifference()


class DateDifference:
	def __init__(self):
		self.type = 'passive'
		self.help = \
'''日期推算：
	用法：
		*算日期 (+/-)(天数)
		*算日期 (年-月-日) (+/-)(天数)
	计算今天或指定日期，加减指定天数得到的日期。'''

	def run(self, gum, msg):
		if not msg['type'] in ['FriendMessage', 'GroupMessage'] \
				or len(msg['messageChain']) <= 1 \
				or msg['messageChain'][1]['type'] != 'Plain' \
				or msg['messageChain'][1]['text'].split()[0] != '*算日期' \
				or len(msg['messageChain'][1]['text'].split()) <= 1:
			return

		with trying():
			splitted = msg['messageChain'][1]['text'].split()
			if splitted[1][0] in '+-':
				t = time.mktime(time.strptime(time.strftime('%Y-%m-%d'), '%Y-%m-%d'))
				days = int(splitted[1][1:])
				if splitted[1][0] == '-':
					days *= -1
			elif splitted[2][0] in '+-':
				t = time.mktime(time.strptime(splitted[1], '%Y-%m-%d'))
				days = int(splitted[2][1:])
				if splitted[2][0] == '-':
					days *= -1

			t += days * 86400
			target = time.strftime('%Y-%m-%d', time.localtime(t))

			chain = MsgChain()
			chain.addPlain(f'结果是{target}哦~')
			autoReply(chain, gum)